<template>
  <!-- 添加或修改盘点计划对话框 -->
  <el-dialog :title="title" v-if="open" v-model="open" width="600px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="90px" style="padding-left: 10%;">
      <el-form-item label="上级分类">
        <el-tree-select v-model="form.parentId" :data="categoryList" @change="categoryChange"
          :props="{ value: 'id', label: 'classifyName', children: 'children' }" value-key="id" placeholder="选择产品分类"
          check-strictly style="width: 400px;" />
      </el-form-item>
      <el-form-item label="分类名称:" prop="classifyName">
        <el-input v-model="form.classifyName"></el-input>
      </el-form-item>
      <el-form-item label="排序:" prop="sort">
        <el-input v-model="form.sort"></el-input>
      </el-form-item>
      <el-form-item label="备注:" prop="remark">
        <el-input type="textarea" :rows="4" v-model="form.remark"></el-input>
      </el-form-item>
      <el-form-item label="状态:" prop="classStatus">
        <el-radio-group v-model="form.classStatus">
          <el-radio label="1">正常</el-radio>
          <el-radio label="0">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import modelMixin from '@/mixins/modelMixin'
import { getCategoryAllList } from '@/api/goods/category'
import category from '@/api/goods/category';
export default {
  name: "addForm",
  mixins: [modelMixin],
  data() {
    return {
      funcObj: category,//方法对象
      open: false,
      title: "添加分类",
      form: {
        classStatus: '1',
        sort: '0'
      },
      rules: {
        classifyName: [
          { required: true, message: "请输入分类名称", trigger: "blur" }
        ]
      },
      categoryList: [],
    }
  },
  methods: {
    add(row) {
      this.open = true;
      this.title = "添加分类"
      this.form = {
        classStatus: '1',
        sort: '0'
      }
      this.form.parentId = row.id || ''
      this.categoryChange(this.form.parentId)
      this.getCategoryAllList()
    },

    /**
     * @编辑
     * @param {*} id 
     */
    edit(row) {
      this.open = true;
      this.title = "编辑分类"
      this.getDetail(row)
      this.getCategoryAllList((data) => {
        data = data.map(item => {
          item.isDisabled = item.id == row.id || item.parentId == row.id
          return item
        })
        this.categoryList = this.handleTree(data, 'id')
      })
    },
    /**
     * @获取所有分类
     */
    getCategoryAllList(callback) {
      getCategoryAllList().then(res => {
        this.categoryList = this.handleTree(res, 'id')
        typeof (callback) === 'function' && callback(res)
      }).catch(err => {
        console.log(err)
      });

    },
    classifyForm() {
      this.form.parentId = this.form.parentId || 0
    },
    categoryChange(value) {
      let item = this.getTreeItem(this.categoryList, value)
      if (item && item.children && item.children.length > 0) {
        this.form.sort = item.children[item.children.length - 1].sort + 10
      }
    },
    // 校验上级分类是都与当前分类重复
    validateRepeat() {
    },
    getTreeItem(list, id) {
      const stack = [...list];
      while (stack.length > 0) {
        const item = stack.pop();
        if (item.id === id) {
          return item;
        }
        if (item.children && item.children.length > 0) {
          stack.push(...item.children);
        }
      }
      return null;
    },

    /**
 * @提交
 * @param {*} close 
 */
    submitForm(close = true) {
      this.$refs.form.validate(valid => {
        if (this.form.id === this.form.parentId) {
          this.$message.error('上级分类不能选择当前分类')
          return
        }
        if (valid) {
          typeof (this.classifyForm) === 'function' && this.classifyForm()
          this.loading = true
          if (this.form.id) {
            this.funcObj.update(this.form).then(res => {
              this.loading = false
              if (res.code == 200) {
                this.$modal.msgSuccess('操作成功')
                if (close) {
                  this.open = false;
                }
                this.$emit('ok');
              } else {
                this.$modal.msgError(res.msg)
              }
            }).catch(err => {
              this.loading = false
              console.log('err :>> ', err);
            })
          } else {
            this.funcObj.add(this.form).then(res => {
              this.loading = false
              if (res.code == 200) {
                this.$modal.msgSuccess('新增成功')
                if (close) {
                  this.open = false;
                }
                this.$emit('ok');
              } else {
                this.$modal.msgError(res.msg)
              }
            }).catch(err => {
              console.log('err :>> ', err);
              this.loading = false
            })
          }

        }
      });
    },

  }
}
</script>

<style lang="scss" scoped></style>